//
//  BCHomeAdCollectionCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCHomeAdCollectionCell.h"

#import "BCAdsDataModel.h"

@interface BCHomeAdCollectionCell ()

@property (nonatomic, strong) UIImageView *adIV;


/**
 *placeholderImage
 */
@property (nonatomic, copy) UIImage *placeholderImage;

@end

@implementation BCHomeAdCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = true;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.adIV = [UIImageView new];
//    self.adIV.backgroundColor = [UIColor generalBackgroundColor];
//    self.adIV.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.adIV];
    [self.adIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
}

- (void)setCellSize:(CGSize)cellSize {
    _cellSize = cellSize;
    if (!CGSizeEqualToSize(cellSize, CGSizeZero)) {
        if (!_placeholderImage) {
            _placeholderImage = [UIImage qmui_imageWithColor:[UIColor clearColor] size:cellSize cornerRadius:0];
            UIImage *logo = LOAD_IMAGE(@"logo_placeholder");
            //
            _placeholderImage = [_placeholderImage qmui_imageWithImageAbove:logo atPoint:CGPointMake(cellSize.width/2-logo.size.width/2, cellSize.height/2-logo.size.height/2)];
        }
    }
}


- (void)setAdModel:(BCAdsDataModel *)adModel {
    _adModel = adModel;
    //[NSURL URLWithString:adModel.image_url]
    self.self.adIV.backgroundColor = [UIColor separatorViewColor];
    [self.adIV sd_setImageWithURL:[NSURL URLWithString:adModel.image_url] placeholderImage:self.placeholderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        if (image) {
//            UIColor *color = [image qmui_averageColor];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                wself.self.adIV.backgroundColor = color;
//            });
//        }
    }];
}

@end
